<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Tree;
use App\Models\PetCategory;
use Dcat\Admin\Layout\Row;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;

class PetCategoryController extends AdminController
{

    public function index(Content $content)
    {
        return $content->header('服务类别')
            ->body(function (Row $row) {
                $tree = new Tree(new PetCategory);
                $tree->branch(function ($branch) {
                    return "{$branch['title']}";
                });
                $row->column(12, $tree);
            });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PetCategory(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('parent_id');
            $grid->column('title');
            $grid->column('order');
            $grid->column('describe');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');


            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PetCategory(), function (Show $show) {
            $show->field('id');
            $show->field('parent_id');
            $show->field('title');
            $show->field('order');
            $show->field('describe');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PetCategory(), function (Form $form) {
            $form->display('id');
            $form->select('parent_id', trans('admin.parent_id'))->options(function ()  {
                return PetCategory::selectOptions();
            })->required();
            $form->text('title');
            $form->number('order');
            $form->markdown('describe');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function getCate(){
        return PetCategory::selectOptions();
    }

    // 定义一个public方法访问图片或文件
    public function getImage()
    {
        if (Str::contains($this->image, '//')) {
            return $this->image;
        }

        return Storage::disk('admin')->url($this->image);
    }
}
